package abstractClass.no2;

import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Filter filter;
		String text;
		int choice;
		Scanner scanner=new Scanner(System.in);
		while (true){
			try{
				System.out.println("请输入字符串");
				text = scanner.nextLine();
				System.out.println("""
						请选择过滤方式
						1.大写转小写
						2.移除空格
						0.退出
						""");
				choice=scanner.nextInt();
				switch (choice){
					case 1:		filter=new Lower();					break;
					case 2:		filter=new SpaceRemoved();			break;
					default:	System.out.println("选择无效");		continue;
					case 0:		System.out.println("欢迎再次使用！");	return;
				}
				System.out.println("处理得");
				System.out.println(filter.filter(text));
			}
			catch (InputMismatchException inputMismatchException){
				System.out.println("选择无效");
			}
		}
	}
}
abstract class Filter{
	abstract String filter(String input);
}
class Lower extends Filter{

	@Override
	String filter(String input) {
		char[] toBeProcessed=input.toCharArray(),
				processed=new char[input.length()];
		int index=0;
		for (char c:toBeProcessed){
			if(c>='A'&&c<='Z')
				processed[index++]= (char) (c-'A'+'a');
			else processed[index++]=c;
		}
		return new String(processed);
	}
}
class SpaceRemoved extends Filter{
	@Override
	String filter(String input) {
		char[] toBeProcessed=input.toCharArray(),
				processed=new char[input.length()];
		int index=0;
		for (char c:toBeProcessed)
			if(c!=' ')	processed[index++]=c;
		return new String(processed);
	}
}
